package com.hspedu.innerclass;

/**
 * @author yulian174
 * @version 1.0
 */
public class Eight {
    public static void main(String[] args) {
        Eight1 eight1 = new Eight1();
        eight1.put(0);
        System.out.println("一共有" + eight1.count + "种排法");
    }
}

class Eight1 {
    int[] arr = new int[8];
    int count = 0;

    public void put(int n) {
        if (n == 8) {
            prin(n);
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            arr[n] = i;
            if (judge(n)) {
                put(n + 1);
            }
        }
    }

    public boolean judge(int n) {
        for (int i = 0; i < n; i++) {
            if (arr[i] == arr[n] || Math.abs(n - i) == Math.abs(arr[i] - arr[n])) {
                return false;
            }
        }
        return true;
    }

    public void prin(int n) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
        System.out.println();
        count++;
    }
}
